// pages/my/my.js
const app = getApp();
const api = require('../../config/api.js');
const https = require('../../utils/request.js');
const user = require('../../utils/user.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabActive: 1,
    userInfo: {},
    modelList: []
  },
  onTabChange(e) {
    let that = this;
    console.log('点击', e)
    // 更新列表
    let index = e.detail.name;
    that.setData({
      tabActive: index
    });
    that.queryThreeModel();
  },
  // 获取三维模型列表
  queryThreeModel() {
    let that = this;
    wx.showLoading()
    let params = {
      openid: wx.getStorageSync('openid'),
      miniprograms: that.data.tabActive
    }
    https.request(api.UserThree, params, 'post').then(res => {
      wx.hideLoading()
      if (res.error_code == 0) {
        that.setData({
          modelList: res.data
        })
      }
    }).catch(err => {
      wx.hideLoading()
      console.log('错误', err)
    })
  },
  goToModel(e) {
    let that = this;
    let item = e.currentTarget.dataset.item;
    let modelSrc = encodeURIComponent('https://ders-1312669922.cos.ap-guangzhou.myqcloud.com/584a68eba826b342ec671f5e6ce21605/22/avatar/model.gltf');
    let videoSrc=encodeURIComponent(item.spimg);
    if (that.data.tabActive == 1) {
      modelSrc = encodeURIComponent(item.xmimg);
      wx.navigateTo({
        url: '/pages/preview/single?modelId=' + item.id + '&modelSrc=' + modelSrc,
      })
    } else {
      modelSrc = encodeURIComponent(item.xcxModel);
      wx.navigateTo({
        url: '/pages/preview/matrix?modelId=' + item.id + '&modelSrc=' + modelSrc+'&videoSrc='+videoSrc,
      })
    }
  },


  // 修改用户信息
  goToUserInfo() {
    wx.navigateTo({
      url: '/pages/my/register/register',
    })
  },

  // 关于我们
  goToAbout() {
    wx.navigateTo({
      // url: '/pages/my/about/about',
      url: '/pages/home/product/product?name=guanyuwomen',
    })
  },

  // 微信客服
  goToService(){
    let that=this;
    wx.openCustomerServiceChat({
      extInfo: {url: ''},
      corpId: '',
      success(res) {}
    })
  },

  async isLoginRegister() {
    let that = this;
    if (app.globalData.hasLogin) {} else {
      let checkReg = await user.checkRegister()
      console.log('验证', checkReg)
      if (!checkReg) {
        wx.navigateBack();
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {

    // let that = this;
    // await that.isLoginRegister();
    // that.setData({
    //   userInfo: wx.getStorageSync('userInfo')
    // })

    // // 回显用户信息
    // console.log('用户信息', that.data.userInfo)

    // that.queryThreeModel()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    let that = this;
    await that.isLoginRegister();
    that.setData({
      userInfo: wx.getStorageSync('userInfo')
    })

    that.queryThreeModel()
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})